package com.ruoyi.boat.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Slf4j
public class TimeCostAspect {

    @Pointcut(value = "@annotation(com.ruoyi.boat.aop.TimeCost)")
    public void timeCost() {

    }

    @Around("timeCost()")
    public Object timeCostAround(ProceedingJoinPoint joinPoint) {
        Object obj = null;
        try {
            long start = System.currentTimeMillis();
            obj = joinPoint.proceed();
            String method = joinPoint.getSignature().getName();
            String className = joinPoint.getSignature().getDeclaringTypeName();
            long cost = System.currentTimeMillis() - start;
            log.info("TimeCost 类: [{}], 方法: [{}], 耗时: {}ms", className, method, cost);
        } catch (Exception e) {
            e.printStackTrace();
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
        return obj;
    }
}
